package com.dabao.util.web;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by card on 16/5/19.
 */
public class ResponseUtils {

    public static final String CODE_SUCCESS = "success"; //成功
    public static final String CODE_ERROR = "error";//失败

    /**
     * 返回成功信息
     * @param data
     * @return
     */
    public static String success(Object data){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("state", CODE_SUCCESS);
        result.put("data", data);
        return JSON.toJSONString(result);
    }

    /**
     * 返回失败信息
     * @param msg
     * @return
     */
    public static String error(Object msg){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("state", CODE_ERROR);
        result.put("data", msg);
        return JSON.toJSONString(result);
    }

    /**
     * 返回grid数据
     * @return
     */
    public static String listData(int total, Object data, String draw){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("draw", draw);
        result.put("recordsTotal", total);
        result.put("recordsFiltered", total);
        result.put("exportURL", "");
        result.put("data", data);
        return JSON.toJSONString(result);
    }

    /**
     * 返回数据时间格式化
     * @param total
     * @param data
     * @param draw
     * @param dateFormat
     * @return
     */
    public static String listData(int total, Object data, String draw, String dateFormat){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("draw", draw);
        result.put("recordsTotal", total);
        result.put("recordsFiltered", total);
        result.put("exportURL", "");
        result.put("data", data);
        return JSON.toJSONStringWithDateFormat(result, dateFormat);
    }
}
